package com.laborer.PTSS.web.servlet;

import com.alibaba.fastjson.JSONObject;
import com.laborer.PTSS.domain.Comment;
import com.laborer.PTSS.domain.Course;
import com.laborer.PTSS.domain.Message;
import com.laborer.PTSS.domain.User;
import com.laborer.PTSS.service.CourseService;
import com.laborer.PTSS.service.impl.CourseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/course/*")
public class CourseServlet extends BaseServlet {

    private static final long serialVersionUID = 4727966330469254582L;

    private CourseService service = new CourseServiceImpl();

    public void getCourseByType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        List<Course> courseList = service.getCourseByType(type);
        writeValue(courseList, response);
    }

    public void getCourseByUid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        List<Course> courseList = service.getCourseByUid(id);
        System.out.println(id);
        System.out.println(courseList);
        writeValue(courseList, response);
    }

    public void getBuyCourseByUid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        List<Course> courseList = service.getBuyCourseByUid(id);
        System.out.println(id);
        System.out.println(courseList);
        writeValue(courseList, response);
    }

    public void offerCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String title = request.getParameter("title");
        String type = request.getParameter("type");
        String price = request.getParameter("price");
        String place = request.getParameter("place");
        String description = request.getParameter("description");
        String username = request.getParameter("username");
//        User user = (User) request.getSession().getAttribute("user");
//        int uid = 0;

        service.saveOne(title, type, price, place, description, username);

        writeValue("123", response);
    }

    public void getDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Course course = service.getDetail(id);
        List<Comment> comment = service.getComment(id);
        JSONObject jo = new JSONObject();
        jo.put("course", course);
        jo.put("comment", comment);
        writeValue(jo, response);
    }

    public void comment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String comment = request.getParameter("comment");
        String id = request.getParameter("id");
        service.comment(comment, id);
        List<Comment> commentList = service.getComment(id);
        writeValue(commentList, response);
    }

    public void getUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<User> userList = service.getUser();
        User user = (User) request.getSession().getAttribute("user");
        List<Message> messageList = new ArrayList<>();
        if (user != null) {
            messageList = service.getCommentUser(user.getUsername());
        }
        JSONObject jo = new JSONObject();
        jo.put("userList", userList);
        jo.put("messageList", messageList);
        writeValue(jo, response);
    }

    public void getUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("un");

        System.out.println(username);
        User user = service.getUserInfo(username);

        JSONObject jo = new JSONObject();
        jo.put("firstname", user.getFirstname());
        jo.put("lastname", user.getFirstname());
        jo.put("username", user.getUsername());
        jo.put("email", user.getEmail());
        jo.put("telephone", user.getTelephone());
        jo.put("sex", user.getSex().equals("M") ? "Male" : "Female");
        writeValue(jo, response);
    }

}
